//
//  DMForgetVC.m
//  DistributionMall
//
//  Created by JHnoplan on 2018/10/14.
//  Copyright © 2018年 JHnoplan. All rights reserved.
//

#import "DMForgetVC.h"

//requset
#import "DMLoginAndRegisterRequest.h"

@interface DMForgetVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UIButton *getCodeBt;
@property (weak, nonatomic) IBOutlet UILabel *didSendLb;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UIButton *completeBt;
@property (weak, nonatomic) IBOutlet UILabel *completeLb;


@end

@implementation DMForgetVC

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self startInit];
}

-(void)startInit {
    if (_flag == 1) {
        self.navigationItem.title = @"设置";
        _completeLb.text = @"完成修改";
        _completeLb.alpha = 0.4;
        _completeBt.enabled = NO;
    } else {
        self.navigationItem.title = @"忘记密码";
    }
    
    _phoneTF.delegate = self;
    _codeTF.delegate = self;
    _passwordTF.delegate = self;
}



//获取验证码
- (IBAction)getCodeClick:(UIButton *)sender {
    //倒计时
    
    
    if (self.phoneTF.text.length != 11) {
        [JLProgressHUDManager showErrorHud:@"请输入正确的手机号码"];
        return;
    }
    
    
    DMLoginAndRegisterRequest * request = [[DMLoginAndRegisterRequest alloc]initPhoneSMSCodeWithPhone:self.phoneTF.text];
    
    //开始倒计时
    [JLProgressHUDManager showLoadingHud];
    MJWeakSelf
    [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
        [JLProgressHUDManager hideHud];
        
        [weakSelf openCountdown];

        
    } failure:^(KBaseModel *errorModel) {
        
        [JLProgressHUDManager showErrorHud:errorModel.msg];
    }];
    
    
}

//完成
- (IBAction)completeClick:(UIButton *)sender {
    NSLog(@"完成");
    
    
    DMLoginAndRegisterRequest * requset = [[DMLoginAndRegisterRequest alloc]initForgetPWDWithPhone:self.phoneTF.text passWord:[self.passwordTF.text md5String] smsCode:self.codeTF.text];
    
    [JLProgressHUDManager showLoadingHud];
    MJWeakSelf
    [requset newStartRequestWithCompletion:^(KBaseModel *baseModel) {
        
        [JLProgressHUDManager showSuccessHud:@"修改成功"];
        if (weakSelf.flag == 1) {
            [[KuserInfo shareInstance]logout];
            
        }else{
            [weakSelf.navigationController popToRootViewControllerAnimated:YES];

        }
        
    } failure:^(KBaseModel *errorModel) {
        [JLProgressHUDManager showErrorHud:errorModel.msg];
    }];
}

#pragma mark ******************** 读秒处理 *******************
// 开启倒计时效果
-(void)openCountdown{
    __block NSInteger time = 59; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(time <= 0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置按钮的样式
                [_getCodeBt setTitle:@"重新发送" forState:UIControlStateNormal];
                _getCodeBt.userInteractionEnabled = YES;
            });
        }else{
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置按钮显示读秒效果
                [_getCodeBt setTitle:[NSString stringWithFormat:@"%.2ds", seconds] forState:UIControlStateNormal];
                _getCodeBt.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}

//监听textfield 3个都输入了才能点击按钮
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    _completeLb.alpha = 1.0;
    if (string.length > 0) {//+
        if (textField == _phoneTF && _codeTF.text.length > 0 && _passwordTF.text.length > 0) {
            _completeBt.enabled = YES;
        }else if (textField == _codeTF && _phoneTF.text.length > 0 && _passwordTF.text.length > 0) {
            _completeBt.enabled = YES;
        }else if (textField == _passwordTF && _codeTF.text.length > 0 && _phoneTF.text.length > 0) {
            _completeBt.enabled = YES;
        }else {
            _completeBt.enabled = NO;
            _completeLb.alpha = 0.4;
        }
    } else {//-
        if (textField == _phoneTF && _phoneTF.text.length > 1 && _codeTF.text.length > 0 && _passwordTF.text.length > 0) {
            _completeBt.enabled = YES;
        }else if (textField == _codeTF && _codeTF.text.length > 1 && _phoneTF.text.length > 0 && _passwordTF.text.length > 0) {
            _completeBt.enabled = YES;
        }else if (textField == _passwordTF && _passwordTF.text.length > 1 && _codeTF.text.length > 0 && _phoneTF.text.length > 0) {
            _completeBt.enabled = YES;
        }else {
            _completeBt.enabled = NO;
            _completeLb.alpha = 0.4;
        }
    }
    return YES;
}

@end
